package com.hengpeng.itfin.persist.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.annotations.common.util.StringHelper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;

import com.hengpeng.common.persist.AbstractDao;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.Projections;
import com.hengpeng.common.persist.query.constants.QueryConstants.QueryType;
import com.hengpeng.itfin.persist.ReceivePlanDao;
import com.hengpeng.itfinbase.persist.ReceivePlan;
import com.hengpeng.itfinbase.persist.ReceivePlan.ReceiveStatus;

@Repository
@Lazy
public class ReceivePlanDaoImpl extends AbstractDao<ReceivePlan> implements ReceivePlanDao {
    @Override
    public List<ReceivePlan> getRepayReceivePlans(Long borrowinfoId, String batchId, Integer curRecNum,
            ReceiveStatus[] status) {
        DynamicQuery query = new DynamicQuery(ReceivePlan.class);
        if (borrowinfoId != null) {
            query.eq("borrowInfoId", borrowinfoId);
        }
        if (!StringHelper.isEmpty(batchId)) {
            query.eq("batchId", batchId);
        }
        if (curRecNum != null) {
            query.eq("curRecNum", curRecNum);
        }
        if (status != null && status.length > 0) {
            query.inArray("status", status);
        }
        List<ReceivePlan> dataList = super.findAllByDynamicQuery(query);
        return dataList == null ? new ArrayList<ReceivePlan>() : dataList;
    }

    @Override
    public List<String> getRepayReceivePlanIds(Long borrowinfoId, String batchId, Integer curRecNum,
            ReceiveStatus[] status) {
        DynamicQuery query = new DynamicQuery(ReceivePlan.class);
        query.addProjection(Projections.property("id"));
        if (borrowinfoId != null) {
            query.eq("borrowInfoId", borrowinfoId);
        }
        if (!StringHelper.isEmpty(batchId)) {
            query.eq("batchId", batchId);
        }
        if (curRecNum != null) {
            query.eq("curRecNum", curRecNum);
        }
        if (status != null && status.length > 0) {
            query.inArray("status", status);
        }
        query.setQueryType(QueryType.JDBC);
        List<String> dataList = (List<String>) super.findByDynamicQuery(query);
        return dataList;
    }

    // @Override
    // public List<ReceivePlan> lockList(Long borrowinfoId, String batchId,
    // Integer curRecNum, ReceiveStatus[] status) {
    // DynamicQuery query = new DynamicQuery(ReceivePlan.class);
    // if (borrowinfoId != null) {
    // query.eq("borrowInfoId", borrowinfoId);
    // }
    // if (!StringHelper.isEmpty(batchId)) {
    // query.eq("batchId", batchId);
    // }
    // if (curRecNum != null) {
    // query.eq("curRecNum", curRecNum);
    // }
    // if (status != null && status.length > 0) {
    // query.inArray("status", status);
    // }
    // return super.findAllByDynamicQuery(query, LockType.LOCK_WAIT);
    // }

    @Override
    public long countPlans(Long borrowinfoId, String batchId, Integer curRecNum, ReceiveStatus[] status) {
        DynamicQuery query = new DynamicQuery(ReceivePlan.class);
        query.addProjection(Projections.count());
        query.setQueryType(QueryType.JDBC);
        if (borrowinfoId != null) {
            query.eq("borrowInfoId", borrowinfoId);
        }
        if (!StringHelper.isEmpty(batchId)) {
            query.eq("batchId", batchId);
        }
        if (curRecNum != null) {
            query.eq("curRecNum", curRecNum);
        }
        if (status != null && status.length > 0) {
            query.inArray("status", status);
        }
        return super.countByDynamicQuery(query);
    }

    @Override
    public int updateStatus(Long id, ReceiveStatus newStatus, ReceiveStatus oldStatus, Date receiveTime, Date updateTime) {
        return super.exeNamedQuery("receivePlan.updateStatus", new Object[] { newStatus, receiveTime, updateTime, id,
                oldStatus });
    }

    @Override
    public int[] batchUpdateStatus(List<String> ids, ReceiveStatus newStatus, ReceiveStatus oldStatus,
            Date receiveTime, Date updateTime) {
        List<List<Object>> parameters = new ArrayList<List<Object>>();
        for (String id : ids) {
            List<Object> values = new ArrayList<Object>();
            values.add(newStatus);
            values.add(receiveTime);
            values.add(updateTime);
            values.add(Long.parseLong(id));
            values.add(oldStatus);
            parameters.add(values);
        }
        return super.exeBatchNamedQuery("receivePlan.batchUpdateStatus", parameters);
    }

}
